﻿using Storage.Core.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Storage.Mapping.AppStorage;
using Storage.Mapping;
using Storage.Entities;
using Storage.Core.Data;

namespace Storage.Services.AppStorage
{
    public class AppStorageService : IAppStorageService
    {
        private const string MODEL_KEY = "Storage.services.appstorage";
        private ICacheManager _cacheManager;
        private IRepository<Entities.AppStorage> _appStorageRepository;

        public AppStorageService(ICacheManager cacheManager,
            IRepository<Entities.AppStorage> appStorageRepository)
        {
            this._cacheManager = cacheManager;
            this._appStorageRepository = appStorageRepository;
        }

        /// <summary>
        /// 获取所有并缓存
        /// </summary>
        /// <returns></returns>
        public List<AppStorageMapping> getAllCache()
        {
            return _cacheManager.get<List<AppStorageMapping>>(MODEL_KEY, 24 * 60, () =>
            {
                var list = _appStorageRepository.Table.ToList();
                if (list.Any())
                    return list.Select(item => item.toModel()).ToList();
                return null;
            });
        }

        /// <summary>
        /// 通过id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AppStorageMapping getById(Guid id)
        {
            var list = getAllCache();
            if (list == null)
                return null;
            return list.FirstOrDefault(o => o.Id == id);
        }

        /// <summary>
        /// 通过appkey获取
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public AppStorageMapping getByAppKey(string appkey)
        {
            var list = getAllCache();
            if (list == null)
                return null;
            return list.FirstOrDefault(o => o.AppKey == appkey);
        }

        public Tuple<bool, string> insertStorage(Entities.AppStorage entity)
        {
            if (_appStorageRepository.Table.Any(o => o.RootDirectory == entity.RootDirectory))
                return new Tuple<bool, string>(false, "根目录已被占用");
            _appStorageRepository.insert(entity);
            _cacheManager.remove(MODEL_KEY);
            return new Tuple<bool, string>(true, "添加成功");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void updateStorage(AppStorageMapping model)
        {
            var item = _appStorageRepository.getById(model.Id);
            if (item == null)
                return;
            item.Name = model.Name;
            item.WaterType = model.WaterType;
            item.Position = model.Position;
            item.Opacity = model.Opacity;
            item.FontColor = model.FontColor;
            item.FontFamily = model.FontFamily;
            item.FontFill = model.FontFill;
            item.FontRotate = model.FontRotate;
            item.FontSize = model.FontSize;
            item.FontText = model.FontText;
            if (model.WaterImage != null)
                item.WaterImage = model.WaterImage;
            _appStorageRepository.update(item);
            _cacheManager.remove(MODEL_KEY); 
        }
    }
}
